
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Signals &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="System check framework" href="checks.html" />
    <link rel="prev" title="Django settings" href="settings.html" />



 
<script src="../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="settings.html" title="Django settings">previous</a>
     |
    <a href="index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="checks.html" title="System check framework">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-signals">
            
  <div class="section" id="s-module-django.dispatch">
<span id="s-signals"></span><span id="module-django.dispatch"></span><span id="signals"></span><h1>Signals<a class="headerlink" href="#module-django.dispatch" title="永久链接至标题">¶</a></h1>
<p>Django includes a &quot;signal dispatcher&quot; which helps decoupled applications get
notified when actions occur elsewhere in the framework. In a nutshell, signals
allow certain <em>senders</em> to notify a set of <em>receivers</em> that some action has
taken place. They're especially useful when many pieces of code may be
interested in the same events.</p>
<p>Django provides a <a class="reference internal" href="../ref/signals.html"><span class="doc">set of built-in signals</span></a> that let user
code get notified by Django itself of certain actions. These include some useful
notifications:</p>
<ul>
<li><p class="first"><a class="reference internal" href="../ref/signals.html#django.db.models.signals.pre_save" title="django.db.models.signals.pre_save"><code class="xref py py-data docutils literal notranslate"><span class="pre">django.db.models.signals.pre_save</span></code></a> &amp;
<a class="reference internal" href="../ref/signals.html#django.db.models.signals.post_save" title="django.db.models.signals.post_save"><code class="xref py py-data docutils literal notranslate"><span class="pre">django.db.models.signals.post_save</span></code></a></p>
<p>Sent before or after a model's <a class="reference internal" href="../ref/models/instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><code class="xref py py-meth docutils literal notranslate"><span class="pre">save()</span></code></a> method
is called.</p>
</li>
<li><p class="first"><a class="reference internal" href="../ref/signals.html#django.db.models.signals.pre_delete" title="django.db.models.signals.pre_delete"><code class="xref py py-data docutils literal notranslate"><span class="pre">django.db.models.signals.pre_delete</span></code></a> &amp;
<a class="reference internal" href="../ref/signals.html#django.db.models.signals.post_delete" title="django.db.models.signals.post_delete"><code class="xref py py-data docutils literal notranslate"><span class="pre">django.db.models.signals.post_delete</span></code></a></p>
<p>Sent before or after a model's <a class="reference internal" href="../ref/models/instances.html#django.db.models.Model.delete" title="django.db.models.Model.delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete()</span></code></a>
method or queryset's <a class="reference internal" href="../ref/models/querysets.html#django.db.models.query.QuerySet.delete" title="django.db.models.query.QuerySet.delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete()</span></code></a>
method is called.</p>
</li>
<li><p class="first"><a class="reference internal" href="../ref/signals.html#django.db.models.signals.m2m_changed" title="django.db.models.signals.m2m_changed"><code class="xref py py-data docutils literal notranslate"><span class="pre">django.db.models.signals.m2m_changed</span></code></a></p>
<p>Sent when a <a class="reference internal" href="../ref/models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a> on a model is changed.</p>
</li>
<li><p class="first"><a class="reference internal" href="../ref/signals.html#django.core.signals.request_started" title="django.core.signals.request_started"><code class="xref py py-data docutils literal notranslate"><span class="pre">django.core.signals.request_started</span></code></a> &amp;
<a class="reference internal" href="../ref/signals.html#django.core.signals.request_finished" title="django.core.signals.request_finished"><code class="xref py py-data docutils literal notranslate"><span class="pre">django.core.signals.request_finished</span></code></a></p>
<p>Sent when Django starts or finishes an HTTP request.</p>
</li>
</ul>
<p>See the <a class="reference internal" href="../ref/signals.html"><span class="doc">built-in signal documentation</span></a> for a complete list,
and a complete explanation of each signal.</p>
<p>You can also <a class="reference internal" href="#defining-and-sending-signals">define and send your own custom signals</a>; see below.</p>
<div class="section" id="s-listening-to-signals">
<span id="listening-to-signals"></span><h2>Listening to signals<a class="headerlink" href="#listening-to-signals" title="永久链接至标题">¶</a></h2>
<p>To receive a signal, register a <em>receiver</em> function using the
<a class="reference internal" href="#django.dispatch.Signal.connect" title="django.dispatch.Signal.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signal.connect()</span></code></a> method. The receiver function is called when the signal
is sent. All of the signal's receiver functions are called one at a time, in
the order they were registered.</p>
<dl class="method">
<dt id="django.dispatch.Signal.connect">
<code class="descclassname">Signal.</code><code class="descname">connect</code>(<em>receiver</em>, <em>sender=None</em>, <em>weak=True</em>, <em>dispatch_uid=None</em>)<a class="headerlink" href="#django.dispatch.Signal.connect" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple">
<li><strong>receiver</strong> -- The callback function which will be connected to this
signal. See <a class="reference internal" href="#receiver-functions"><span class="std std-ref">Receiver functions</span></a> for more information.</li>
<li><strong>sender</strong> -- Specifies a particular sender to receive signals from. See
<a class="reference internal" href="#connecting-to-specific-signals"><span class="std std-ref">Connecting to signals sent by specific senders</span></a> for more information.</li>
<li><strong>weak</strong> -- Django stores signal handlers as weak references by
default. Thus, if your receiver is a local function, it may be
garbage collected. To prevent this, pass <code class="docutils literal notranslate"><span class="pre">weak=False</span></code> when you call
the signal's <code class="docutils literal notranslate"><span class="pre">connect()</span></code> method.</li>
<li><strong>dispatch_uid</strong> -- A unique identifier for a signal receiver in cases
where duplicate signals may be sent. See
<a class="reference internal" href="#preventing-duplicate-signals"><span class="std std-ref">Preventing duplicate signals</span></a> for more information.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<p>Let's see how this works by registering a signal that
gets called after each HTTP request is finished. We'll be connecting to the
<a class="reference internal" href="../ref/signals.html#django.core.signals.request_finished" title="django.core.signals.request_finished"><code class="xref py py-data docutils literal notranslate"><span class="pre">request_finished</span></code></a> signal.</p>
<div class="section" id="s-receiver-functions">
<span id="s-id1"></span><span id="receiver-functions"></span><span id="id1"></span><h3>Receiver functions<a class="headerlink" href="#receiver-functions" title="永久链接至标题">¶</a></h3>
<p>First, we need to define a receiver function. A receiver can be any Python
function or method:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_callback</span><span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Request finished!&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Notice that the function takes a <code class="docutils literal notranslate"><span class="pre">sender</span></code> argument, along with wildcard
keyword arguments (<code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>); all signal handlers must take these arguments.</p>
<p>We'll look at senders <a class="reference internal" href="#connecting-to-signals-sent-by-specific-senders">a bit later</a>, but right now look at the <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>
argument. All signals send keyword arguments, and may change those keyword
arguments at any time. In the case of
<a class="reference internal" href="../ref/signals.html#django.core.signals.request_finished" title="django.core.signals.request_finished"><code class="xref py py-data docutils literal notranslate"><span class="pre">request_finished</span></code></a>, it's documented as sending no
arguments, which means we might be tempted to write our signal handling as
<code class="docutils literal notranslate"><span class="pre">my_callback(sender)</span></code>.</p>
<p>This would be wrong -- in fact, Django will throw an error if you do so. That's
because at any point arguments could get added to the signal and your receiver
must be able to handle those new arguments.</p>
</div>
<div class="section" id="s-connecting-receiver-functions">
<span id="s-id2"></span><span id="connecting-receiver-functions"></span><span id="id2"></span><h3>Connecting receiver functions<a class="headerlink" href="#connecting-receiver-functions" title="永久链接至标题">¶</a></h3>
<p>There are two ways you can connect a receiver to a signal. You can take the
manual connect route:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.signals</span> <span class="kn">import</span> <span class="n">request_finished</span>

<span class="n">request_finished</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">my_callback</span><span class="p">)</span>
</pre></div>
</div>
<p>Alternatively, you can use a <a class="reference internal" href="#django.dispatch.receiver" title="django.dispatch.receiver"><code class="xref py py-func docutils literal notranslate"><span class="pre">receiver()</span></code></a> decorator:</p>
<dl class="function">
<dt id="django.dispatch.receiver">
<code class="descname">receiver</code>(<em>signal</em>)<a class="headerlink" href="#django.dispatch.receiver" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><strong>signal</strong> -- A signal or a list of signals to connect a function to.</td>
</tr>
</tbody>
</table>
</dd></dl>

<p>Here's how you connect with the decorator:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.signals</span> <span class="kn">import</span> <span class="n">request_finished</span>
<span class="kn">from</span> <span class="nn">django.dispatch</span> <span class="kn">import</span> <span class="n">receiver</span>

<span class="nd">@receiver</span><span class="p">(</span><span class="n">request_finished</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_callback</span><span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Request finished!&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Now, our <code class="docutils literal notranslate"><span class="pre">my_callback</span></code> function will be called each time a request finishes.</p>
<div class="admonition-where-should-this-code-live admonition">
<p class="first admonition-title">Where should this code live?</p>
<p>Strictly speaking, signal handling and registration code can live anywhere
you like, although it's recommended to avoid the application's root module
and its <code class="docutils literal notranslate"><span class="pre">models</span></code> module to minimize side-effects of importing code.</p>
<p class="last">In practice, signal handlers are usually defined in a <code class="docutils literal notranslate"><span class="pre">signals</span></code>
submodule of the application they relate to. Signal receivers are
connected in the <a class="reference internal" href="../ref/applications.html#django.apps.AppConfig.ready" title="django.apps.AppConfig.ready"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ready()</span></code></a> method of your
application configuration class. If you're using the <a class="reference internal" href="#django.dispatch.receiver" title="django.dispatch.receiver"><code class="xref py py-func docutils literal notranslate"><span class="pre">receiver()</span></code></a>
decorator, import the <code class="docutils literal notranslate"><span class="pre">signals</span></code> submodule inside
<a class="reference internal" href="../ref/applications.html#django.apps.AppConfig.ready" title="django.apps.AppConfig.ready"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ready()</span></code></a>.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">The <a class="reference internal" href="../ref/applications.html#django.apps.AppConfig.ready" title="django.apps.AppConfig.ready"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ready()</span></code></a> method may be executed more than
once during testing, so you may want to <a class="reference internal" href="#preventing-duplicate-signals"><span class="std std-ref">guard your signals from
duplication</span></a>, especially if you're planning
to send them within tests.</p>
</div>
</div>
<div class="section" id="s-connecting-to-signals-sent-by-specific-senders">
<span id="s-connecting-to-specific-signals"></span><span id="connecting-to-signals-sent-by-specific-senders"></span><span id="connecting-to-specific-signals"></span><h3>Connecting to signals sent by specific senders<a class="headerlink" href="#connecting-to-signals-sent-by-specific-senders" title="永久链接至标题">¶</a></h3>
<p>Some signals get sent many times, but you'll only be interested in receiving a
certain subset of those signals. For example, consider the
<a class="reference internal" href="../ref/signals.html#django.db.models.signals.pre_save" title="django.db.models.signals.pre_save"><code class="xref py py-data docutils literal notranslate"><span class="pre">django.db.models.signals.pre_save</span></code></a> signal sent before a model gets saved.
Most of the time, you don't need to know when <em>any</em> model gets saved -- just
when one <em>specific</em> model is saved.</p>
<p>In these cases, you can register to receive signals sent only by particular
senders. In the case of <a class="reference internal" href="../ref/signals.html#django.db.models.signals.pre_save" title="django.db.models.signals.pre_save"><code class="xref py py-data docutils literal notranslate"><span class="pre">django.db.models.signals.pre_save</span></code></a>, the sender
will be the model class being saved, so you can indicate that you only want
signals sent by some model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db.models.signals</span> <span class="kn">import</span> <span class="n">pre_save</span>
<span class="kn">from</span> <span class="nn">django.dispatch</span> <span class="kn">import</span> <span class="n">receiver</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">MyModel</span>


<span class="nd">@receiver</span><span class="p">(</span><span class="n">pre_save</span><span class="p">,</span> <span class="n">sender</span><span class="o">=</span><span class="n">MyModel</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_handler</span><span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">my_handler</span></code> function will only be called when an instance of <code class="docutils literal notranslate"><span class="pre">MyModel</span></code>
is saved.</p>
<p>Different signals use different objects as their senders; you'll need to consult
the <a class="reference internal" href="../ref/signals.html"><span class="doc">built-in signal documentation</span></a> for details of each
particular signal.</p>
</div>
<div class="section" id="s-preventing-duplicate-signals">
<span id="s-id3"></span><span id="preventing-duplicate-signals"></span><span id="id3"></span><h3>Preventing duplicate signals<a class="headerlink" href="#preventing-duplicate-signals" title="永久链接至标题">¶</a></h3>
<p>In some circumstances, the code connecting receivers to signals may run
multiple times. This can cause your receiver function to be registered more
than once, and thus called as many times for a signal event. For example, the
<a class="reference internal" href="../ref/applications.html#django.apps.AppConfig.ready" title="django.apps.AppConfig.ready"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ready()</span></code></a> method may be executed more than once
during testing. More generally, this occurs everywhere your project imports the
module where you define the signals, because signal registration runs as many
times as it is imported.</p>
<p>If this behavior is problematic (such as when using signals to
send an email whenever a model is saved), pass a unique identifier as
the <code class="docutils literal notranslate"><span class="pre">dispatch_uid</span></code> argument to identify your receiver function. This
identifier will usually be a string, although any hashable object will
suffice. The end result is that your receiver function will only be
bound to the signal once for each unique <code class="docutils literal notranslate"><span class="pre">dispatch_uid</span></code> value:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.signals</span> <span class="kn">import</span> <span class="n">request_finished</span>

<span class="n">request_finished</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">my_callback</span><span class="p">,</span> <span class="n">dispatch_uid</span><span class="o">=</span><span class="s2">&quot;my_unique_identifier&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-defining-and-sending-signals">
<span id="defining-and-sending-signals"></span><h2>Defining and sending signals<a class="headerlink" href="#defining-and-sending-signals" title="永久链接至标题">¶</a></h2>
<p>Your applications can take advantage of the signal infrastructure and provide
its own signals.</p>
<div class="admonition-when-to-use-custom-signals admonition">
<p class="first admonition-title">When to use custom signals</p>
<p class="last">Signals are implicit function calls which make debugging harder. If the
sender and receiver of your custom signal are both within your project,
you're better off using an explicit function call.</p>
</div>
<div class="section" id="s-defining-signals">
<span id="defining-signals"></span><h3>Defining signals<a class="headerlink" href="#defining-signals" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.dispatch.Signal">
<em class="property">class </em><code class="descname">Signal</code><a class="headerlink" href="#django.dispatch.Signal" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>All signals are <a class="reference internal" href="#django.dispatch.Signal" title="django.dispatch.Signal"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.dispatch.Signal</span></code></a> instances.</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">django.dispatch</span>

<span class="n">pizza_done</span> <span class="o">=</span> <span class="n">django</span><span class="o">.</span><span class="n">dispatch</span><span class="o">.</span><span class="n">Signal</span><span class="p">()</span>
</pre></div>
</div>
<p>This declares a <code class="docutils literal notranslate"><span class="pre">pizza_done</span></code> signal.</p>
</div>
<div class="section" id="s-sending-signals">
<span id="sending-signals"></span><h3>Sending signals<a class="headerlink" href="#sending-signals" title="永久链接至标题">¶</a></h3>
<p>There are two ways to send signals in Django.</p>
<dl class="method">
<dt id="django.dispatch.Signal.send">
<code class="descclassname">Signal.</code><code class="descname">send</code>(<em>sender</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.dispatch.Signal.send" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="django.dispatch.Signal.send_robust">
<code class="descclassname">Signal.</code><code class="descname">send_robust</code>(<em>sender</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.dispatch.Signal.send_robust" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>To send a signal, call either <a class="reference internal" href="#django.dispatch.Signal.send" title="django.dispatch.Signal.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signal.send()</span></code></a> (all built-in signals use
this) or <a class="reference internal" href="#django.dispatch.Signal.send_robust" title="django.dispatch.Signal.send_robust"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signal.send_robust()</span></code></a>. You must provide the <code class="docutils literal notranslate"><span class="pre">sender</span></code> argument
(which is a class most of the time) and may provide as many other keyword
arguments as you like.</p>
<p>For example, here's how sending our <code class="docutils literal notranslate"><span class="pre">pizza_done</span></code> signal might look:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PizzaStore</span><span class="p">:</span>
    <span class="o">...</span>

    <span class="k">def</span> <span class="nf">send_pizza</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">toppings</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
        <span class="n">pizza_done</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">toppings</span><span class="o">=</span><span class="n">toppings</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">size</span><span class="p">)</span>
        <span class="o">...</span>
</pre></div>
</div>
<p>Both <code class="docutils literal notranslate"><span class="pre">send()</span></code> and <code class="docutils literal notranslate"><span class="pre">send_robust()</span></code> return a list of tuple pairs
<code class="docutils literal notranslate"><span class="pre">[(receiver,</span> <span class="pre">response),</span> <span class="pre">...</span> <span class="pre">]</span></code>, representing the list of called receiver
functions and their response values.</p>
<p><code class="docutils literal notranslate"><span class="pre">send()</span></code> differs from <code class="docutils literal notranslate"><span class="pre">send_robust()</span></code> in how exceptions raised by receiver
functions are handled. <code class="docutils literal notranslate"><span class="pre">send()</span></code> does <em>not</em> catch any exceptions raised by
receivers; it simply allows errors to propagate. Thus not all receivers may
be notified of a signal in the face of an error.</p>
<p><code class="docutils literal notranslate"><span class="pre">send_robust()</span></code> catches all errors derived from Python's <code class="docutils literal notranslate"><span class="pre">Exception</span></code> class,
and ensures all receivers are notified of the signal. If an error occurs, the
error instance is returned in the tuple pair for the receiver that raised the error.</p>
<p>The tracebacks are present on the <code class="docutils literal notranslate"><span class="pre">__traceback__</span></code> attribute of the errors
returned when calling <code class="docutils literal notranslate"><span class="pre">send_robust()</span></code>.</p>
</div>
</div>
<div class="section" id="s-disconnecting-signals">
<span id="disconnecting-signals"></span><h2>Disconnecting signals<a class="headerlink" href="#disconnecting-signals" title="永久链接至标题">¶</a></h2>
<dl class="method">
<dt id="django.dispatch.Signal.disconnect">
<code class="descclassname">Signal.</code><code class="descname">disconnect</code>(<em>receiver=None</em>, <em>sender=None</em>, <em>dispatch_uid=None</em>)<a class="headerlink" href="#django.dispatch.Signal.disconnect" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>To disconnect a receiver from a signal, call <a class="reference internal" href="#django.dispatch.Signal.disconnect" title="django.dispatch.Signal.disconnect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signal.disconnect()</span></code></a>. The
arguments are as described in <a class="reference internal" href="#django.dispatch.Signal.connect" title="django.dispatch.Signal.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signal.connect()</span></code></a>. The method returns
<code class="docutils literal notranslate"><span class="pre">True</span></code> if a receiver was disconnected and <code class="docutils literal notranslate"><span class="pre">False</span></code> if not.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">receiver</span></code> argument indicates the registered receiver to disconnect. It
may be <code class="docutils literal notranslate"><span class="pre">None</span></code> if <code class="docutils literal notranslate"><span class="pre">dispatch_uid</span></code> is used to identify the receiver.</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Signals</a><ul>
<li><a class="reference internal" href="#listening-to-signals">Listening to signals</a><ul>
<li><a class="reference internal" href="#receiver-functions">Receiver functions</a></li>
<li><a class="reference internal" href="#connecting-receiver-functions">Connecting receiver functions</a></li>
<li><a class="reference internal" href="#connecting-to-signals-sent-by-specific-senders">Connecting to signals sent by specific senders</a></li>
<li><a class="reference internal" href="#preventing-duplicate-signals">Preventing duplicate signals</a></li>
</ul>
</li>
<li><a class="reference internal" href="#defining-and-sending-signals">Defining and sending signals</a><ul>
<li><a class="reference internal" href="#defining-signals">Defining signals</a></li>
<li><a class="reference internal" href="#sending-signals">Sending signals</a></li>
</ul>
</li>
<li><a class="reference internal" href="#disconnecting-signals">Disconnecting signals</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="settings.html"
                        title="上一章">Django settings</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="checks.html"
                        title="下一章">System check framework</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/topics/signals.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="settings.html" title="Django settings">previous</a>
     |
    <a href="index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="checks.html" title="System check framework">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>